<?php
  $row=$this->row;
  $clients=$this->clients;
  $clientsArray;

  foreach($clients as $key)
  {
   $clientsArray[$key['mag_cliente']]=$key['mag_cdesc'];
  }
?>
<?php require_once("imports.php"); ?>
<?php require_once("funciones.php"); ?>

<script>
	$(document).ready(function() {
		$( "#btnFacturas" ).click(function() {
			$( "#facturas" ).load("admin/mag_facturas.php");
			$( "#facturas" ).dialog( "open" );
		});
		
		$( "#btnGastos" ).click(function() {
			$( "#gastos" ).load("mag_edit_spend.php");
			$( "#gastos" ).dialog( "open" );
		});
		
		$( "#facturas" ).dialog({
			autoOpen: false,
			height: 540,
			width: 600,
			modal: true,
			open: 	function(){
			$(':input','#frmFacturas')
			 .not(':button, :submit, :reset, :hidden')
			 .val('')
			 .removeAttr('checked')
			 .removeAttr('selected');
			 $('#Info').html("");
			}
		});

		$( "#gastos" ).dialog({
			autoOpen: false,
			height: 540,
			width: 600,
			modal: true,
			open: 	function(){
			$(':input','#frmFacturas')
			 .not(':button, :submit, :reset, :hidden')
			 .val('')
			 .removeAttr('checked')
			 .removeAttr('selected');
			 $('#Info').html("");
			}
		});

		$( "#mag_pago" ).datepicker(
			{
				showOn: "button",
				buttonImage: "images/calendar.gif",
				buttonImageOnly: true,
				dateFormat: 'dd/mm/yy',
				showButtonPanel: true,
				changeMonth: true,
				changeYear: true
			});
      
    $("input[name='mag_currency']").change(function(){
      if($("input[name='mag_currency']:checked").val()=="D")
        $("#mag_cambioD").show();
      else
        $("#mag_cambioD").hide();
    });
    
    $('#add_mag').click(function(){
      var par=document.getElementById("num_mag");
      var newdiv=document.createElement("div");
      if($(this).parent().prev().find('#mag_cliente').val()!='')
      {
        newdiv.innerHTML='<div style="float:left">'+$(this).parent().prev().html()+'</div><div style="float:left"><input type="button" onclick="javascript:removeElem(this);calcCot();" value="Quitar cotización" /></div><div style="clear:both"></div>';
        $(newdiv).find('div').find('#mag_cliente').val($(this).parent().prev().find('#mag_cliente').val());
        par.appendChild(newdiv);
        $('#selectClient').attr('disabled', 'true');
      }
      else
      {
        alert('Seleccione un cliente antes de continuar');
      }
    });

    $('#add_cot').click(function(){
      var par=document.getElementById("cotizaciones");
      var newdiv=document.createElement("div");
      newdiv.innerHTML='<div style="float:left">'+$(this).parent().prev().html()+'</div><div style="float:left"><input type="button" onclick="javascript:removeElem(this);calcCot();" value="Quitar cotización" /></div><div style="clear:both"></div>';
      par.appendChild(newdiv);
    });

    $('#add_buy').click(function(){
      var par=document.getElementById("compras");
      var newdiv=document.createElement("div");
      newdiv.innerHTML='<div>'+$(this).parent().prev().html()+'</div><div style="float:left"><input type="button" onclick="javascript:removeElem(this);calcBuy();" value="Quitar cotización" /></div><div style="clear:both"></div>';
      par.appendChild(newdiv);
      var x=0;
      $(".mail_group").each(function(){
        $(this).find(".mag_mail").attr('name', 'mag_mail['+x+']');
        x++;
      });
    });

		$('#selectClient').change(function() {
			$('#mag_cliente').val($('#selectClient').val());
		});
		$('#selectClient').keyup(function() {
			$('#mag_cliente').val($('#selectClient').val());
		});

	});

</script>
<form action="/ots/new" method="POST" name="registro" class="style2">
  <table align='center' width='100%' border=0><tr><td bgcolor='#FF0000'><font color='#FFFFFF'><b>Capturar nueva orden de trabajo</b></font></td></tr></table><p>
	<?php echo ($auth["u"]==1 ? '<input class="smallbutton" type="button" value="Modificar una OT" onclick="location=\'mag_search.php?op=update&searchr=3\'" \/>' : '') ?> <?php echo ($auth["u"]==1 ? '<input class="smallbutton" type="button" id="btnFacturas" value="Modificar facturas" \/>' : '') ?> <?php echo ($auth["u"]==1 ? '<input class="smallbutton" type="button" id="btnGastos" value="Modificar gastos" \/><br>' : '') ?>
	<?php echo ($auth["d"]==1 ? '<input class="smallbutton" type="button" value="Aprobar una OT" onclick="location=\'mag_search.php?&searchr=3&approv=1\'" \/> <input class="smallbutton" type="button" value="Abrir una OT" onclick="location=\'mag_search.php?&searchr=3&open=1\'" \/> <input class="smallbutton" type="button" value="Cerrar una OT" onclick="location=\'mag_search.php?&searchr=3&open=2\'" \/>' : '')?><p>
  <table width="100%" border="0">
    <tr>
      <td width="20%" valign="top"><div align="right">Fecha de elaboración</div></td>
      <td><div align="left">
        <strong><?php echo date("d/m/Y") ?></strong>
      </div></td>
    </tr>
    <tr>
      <td width="20%" valign="top"><div align="right">Número de folio</div></td>
      <td><div align="left">
        <?php echo $this->htmlObjects('folio','mag_folio',str_pad($row["mag_folio"],4,"0",STR_PAD_LEFT))?>/<?php echo $this->htmlObjects('folio','mag_year',$row["mag_year"])?>
      </div></td>
    </tr>
    <tr>
      <td width="20%" valign="top"><div align="right">Cotización</div></td>
      <td><div style="float:left">
        Valor $<?php echo $this->htmlObjects('number','mag_cotizacion_valor[]',null,array('onkeyup'=>'calcCot()')); ?> No. de cotización<?php echo $this->htmlObjects('text','mag_cotizacion[]'); ?></div>
        <div style="float:left"><?php echo $this->formButton('add_cot','Añadir cotización');?></div>
      <div style="clear:both"></div>
      <div id="cotizaciones"></div>
      <div style="clear:both"></div>
      <div id="total_cotizaciones" style="font-weight:bold">Total de cotización $0.00</div></td>
    </tr>
    <tr>
      <td width="20%" valign="top"><div align="right">Orden de compra</div></td>
      <td><div><div class="mail_group">
        <?php echo $this->formRadio('mag_mail[0]',0,array('onclick'=>'javascript:num_compra(1)','class'=>'mag_mail'),
          array(
              0 => 'Orden',
              1 => 'Correo',
          )
         ); ?>
        </div>
          <div style="clear:both"></div>
          <div style="float:left">Valor $<?php echo $this->htmlObjects('number','mag_compra_valor[]',null,array('onkeyup'=>'calcBuy()')); ?> No. de orden de c.<?php echo $this->htmlObjects('text','mag_compra[]'); ?></div>
        </div>
        <div style="float:left"><?php echo $this->formButton('add_buy','Añadir orden de compra');?></div>
      <div style="clear:both"></div>
      <div id="compras"></div>
      <div style="clear:both"></div>
      <div id="total_compras" style="font-weight:bold">Total de órdenes de compra $0.00</div></td>
    </tr>
    <tr>
      <td width="20%" valign="top"><div align="right"></div></td>
      <td><div align="left">
        <?php echo $this->formRadio('mag_currency',$row['mag_currency'],null,
          array(
              'P' => 'Pesos',
              'D' => 'Dólares',
          ),'&nbsp;'
         ); ?>
        </div>
        <div id="mag_cambioD" name="mag_cambioD" style="display:none;">
          Tipo de cambio actual $<?php echo $this->htmlObjects('small_number','mag_cambio'); ?>
        </div>
        <div>
        <?php echo $this->formCheckbox('mag_iva',null,($row["mag_iva"]=="1" ? array('checked'=>'true'):''));?>
        No causa IVA
		</div>
    </tr>
    <tr>
      <td width="20%" valign="top"><div align="right">Número MAG</div></td>
      <td>
        <?php echo $this->formSelect('selectClient',null,null,array_merge(array(''=>'Seleccione un cliente de la lista'),$clientsArray)); ?>
        <br/>
      <div>
        <div style="float:left">
        <?php echo $this->htmlObjects('folio','mag_cliente[]')?><?php echo $this->htmlObjects('small_number','mag_numero[]'); ?>
        </div>
        <div style="float:left">
          <?php echo $this->formButton('add_mag','Añadir número MAG');?>
        </div>
      </div>
      <div style="clear:both"></div>
      <div id="num_mag"></div></td>
    </tr>
    <tr>
      <td width="20%" valign="top"><div align="right">Comentarios</div></td>
      <td><div align="left">
        <?php echo $this->htmlObjects('comment','mag_comment',$row['mag_comment']) ?>
      	</div>
			</td>
    </tr>
    <tr>
      <td width="20%" valign="top"><div align="right">Fecha de pago programada</div></td>
      <td><div align="left">
          <?php echo $this->htmlObjects('date','mag_pago'); ?>
      	</div>
			</td>
    </tr>
  </table>
  
<p>
  <input type="hidden" name="optype" value="i" />
<table align='center' width='100%' border=0><tr><td bgcolor='#FF0000'><font color='#FFFFFF'><b>&nbsp;</b></font></td></tr></table><p>
	
	<div style='position:relative; font-size:10px; width:100px; top:25px; left:50px;'>
		<center><a href="javascript:document.registro.submit();"><img src="/images/save.png" alt="Guardar datos" title="Guardar datos" border="0"><br>Guardar nueva OT</a></center>
	</div>
</p>
</form>
